package model.wrap.type;

import java.io.File;
import java.util.ArrayList;

import model.wrap.node.Node;
import models.ByteContent;

public class ByteElement
{
    protected models.ByteContent element;
    
    public ByteElement(Node node)
    {
	this.element = new ByteContent();
	this.element.setId_node(node.getId());
	this.element.setId_structure(node.getStructure().getId());
	this.element.setId_type(node.getType().getID());
	//TODO какое имя записывать?
	this.element.setName("def name");
    }
    
    public ByteElement(Long id)
    {
	try
	{
	    this.element = ByteContent.getById(id);
	}
	catch (Exception e)
	{
	    // TODO: handle exception
	}
    }
    
    public Long getId()
    {
	return this.element.getId();
    }
    
    public void save()
    {
	this.element.save();
    }
    
    /**
     * Create Byte element with default value.
     * @param node
     * @return Return byte element.
     */
    public static ByteElement createDefaultValueForNode(Node node)
    {
	ByteElement element = new ByteElement(node);
	element.save();
	return element;
    }
    
    public byte[] getContent()
    {
	return this.element.getContent();
    }
    
    public Node getNode()
    {
	return new Node(this.element.getId_node());
    }
    
    /**
     * @author pocteg
     * @return
     */
    public String getTitle()
    {
	ArrayList<String> ids = this.getNode().getContentsId();
	ArrayList<String> titles = this.getNode().getContentsTypeTitle();
	
	for (int i=0; i<ids.size(); i++)
	{
	    if(ids.get(i).equals(this.getId().toString()) && this.getNode().getType().getArrayTypes().get(i).equals(Type.BYTE))
	    {
		return titles.get(i);
	    }
	}
	
	return "ERROR!";
    }
    
    public void setFile(String name, String type, byte[] file)
    {
	this.element.setType_of(type);
	this.element.setName(name);
	this.element.setContent(file);
    }
    
    public String getContentType()
    {
	return this.element.getType_of();
    }
    
    public String getFileName()
    {
	return this.element.getName();
    }
    
    public void update()
    {
	this.element.update();
    }
}
